import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 使用vite加载scss文件
        additionalData: `
        @use "@/styles/element/index.scss" as *; 
        `,
        api: 'modern-compiler'
      }
    }
  },
  build: {
    target: 'es2017',
    minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
    manifest: false, // 是否产出manifest.json
    sourcemap: false, // 是否产出sourcemap.json
    outDir: 'dist' // 产出目录
  },
  server: {
    // 如果使用docker-compose开发模式，设置为false
    open: true,
    port: Number(process.env.VITE_CLI_PORT),
    proxy: {
      // 把key的路径代理到target位置
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      // :${process.env.VITE_SERVER_PORT}
      [process.env.VITE_BASE_API as string]: {
        // 需要代理的路径   例如 '/api'
        // target: `http://10.10.10.181:8866`, // 代理到 目标路径
        target: `${process.env.VITE_BASE_PATH}`,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp('^' + process.env.VITE_BASE_API), '')
      },
      '/sse': {
        // 需要代理的路径   例如 '/api'
        // target: `http://10.10.7.222:8866`, // 代理到 目标路径
        target: `${process.env.VITE_BASE_PATH}`,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp('^/sse'), '')
      }
    }
  }
})
